var Private=require('../models/privateModel.js');
// var GuestPrePlan=require('../models/guestPrePlanModel.js');

var code=require('../txtCode/code.js');
var moment=require('moment');


// var machineNumber=require('../txtCode/machineNumber.js');
var async=require('async');

var parseXlsx=require('excel');

var excelExport=require('excel-export');

//操作顾少的东西
var nodeXlsx=require('node-xlsx');
var fs=require('fs');
var Guest=require('../models/guestModel.js');

module.exports={
	PrivateAdd:function(req,res,next){
		if(req.session){
			
			var data=req.body;
			console.log(data);
			Private
			.find({
					assistant:data.assistant,
					schedule:
						{      
							$gte:moment(data.year+'-'+
								data.month+'-'+
								data.day+'T'+
								data.hour+':'+
								data.minute,moment.ISO_8601).subtract(6,'hours'),
							$lt:moment(data.year+'-'+
								data.month+'-'+
								data.day+'T'+
								data.hour+':'+
								data.minute,moment.ISO_8601).subtract(4,'hours')
						}
					})
			.count(function(err,count){
				if(err) {
					console.log(err);
					return;
				}
				console.log(count);
				if(count>=1){
					res.send({
						code:[1001,'failed,该使时间段有人']
					});
				}else{
					console.log(data.schedule);
					data.schedule=moment(data.year+'-'+
								 data.month+'-'+
								 data.day+'T'+
								 data.hour+':'+
								 data.minute,moment.ISO_8601).subtract(5,'hours');

					var oNewPrivate=new Private(data);
						// var oNewPrivate=new Private();
					 	oNewPrivate.save(function(err){
					 		if(err) {console.log(err);
					 		
							}
						}).then(
					 		function(data){
					 			res.send({
					 				code:[1000,'success'],
					 				});	
					 			},
					 		function(err){
					 			console.log(err);
					 			res.send({code:[1001,'failed']});
						});//save()
				};//if(count>=1)
			});//Private
		};//if(req.session)
	},//PrivateAdd()
	PrivateSearch:function(req,res,next){
		if(req.session){
			var data=req.body;
			Private
			.find({schedule:
					{
						$gte:moment('2016-01-01T07:32:00',moment.ISO_8601).subtract(5,'hours'),
					 	$lt:moment('2016-01-01T09:32:00',moment.ISO_8601).subtract(5,'hours')
					}
				 })
			.count(function(err,count){
				if(count>=6){
					res.send({
						count:count,
						data:'此时段已经约满'
					});
				}else{
					res.send({
						count:count,
						data:'可约'
					});
				}
			})
			// .then(
			// 	function(data){
		 // 			res.send({data:data});	
		 // 			});

		};//if(req.session)
	},
	SearchCondition:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			if(!data) return;
			

		};//if(req.session)
	},//SearchCondition
	ScheduleSearch:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			var year=data.schedule.year;
			var month=data.schedule.month;
			var day=data.schedule.day;
			if(parseInt(month)<10){
				month=month+'';
				month="0"+month;
				console.log(month);
			}
			if(parseInt(day)<10){
				day=day+'';
				day="0"+day;
				console.log(day);
			}
			console.log(moment.utc(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601));
			console.log(moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).format('L')).subtract(5,'hours').toDate());
			console.log(moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(7,'days').format('L')).subtract(5,'hours').toDate());
			async.waterfall(
			[
				function(cb){
					Private
					.aggregate(
						{
							$match:{
								schedule:{
									$gte:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).format('L')).subtract(5,'hours').toDate(),
									$lt:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(1,'days').format('L')).subtract(5,'hours').toDate()
								}
							}//$match
						},
						{
							$project:{
								guestname:1,
								mobile:1,
								schedule:1,
								assistant:1
							}
						},
						{
							$sort:{
								schedule:1
							}
						},
						function(err,docs){
							if(err) {
								console.log(err);
								return;
						};//if(err)
						cb(null,docs);
					}

						);//aggregate()
				
			},
				function(data0,cb){
					Private
					.aggregate(
						{
							$match:{
								schedule:{
									$gte:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(1,'days').format('L')).subtract(5,'hours').toDate(),
									$lt:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(2,'days').format('L')).subtract(5,'hours').toDate()
								}
							}//$match
						},
						{
							$project:{
								guestname:1,
								mobile:1,
								schedule:1,
								assistant:1
							}
						},
						{
							$sort:{
								schedule:1
							}
						},
						function(err,docs){
							if(err) {
								console.log(err);
								return;
						};//if(err)
						cb(null,data0,docs);
					}

						);//aggregate()
			},
				function(data0,data1,cb){
					Private
					.aggregate(
						{
							$match:{
								schedule:{
									$gte:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(2,'days').format('L')).subtract(5,'hours').toDate(),
									$lt:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(3,'days').format('L')).subtract(5,'hours').toDate()
								}
							}//$match
						},
						{
							$project:{
								guestname:1,
								mobile:1,
								schedule:1,
								assistant:1
							}
						},
						{
							$sort:{
								schedule:1
							}
						},
						function(err,docs){
							if(err) {
								console.log(err);
								return;
						};//if(err)
						cb(null,data0,data1,docs);
					}

						);//aggregate()
			},
		
					function(data0,data1,data2,cb){
					Private
					.aggregate(
						{
							$match:{
								schedule:{
									$gte:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(3,'days').format('L')).subtract(5,'hours').toDate(),
									$lt:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(4,'days').format('L')).subtract(5,'hours').toDate()
								}
							}//$match
						},
						{
							$project:{
								guestname:1,
								mobile:1,
								schedule:1,
								assistant:1
							}
						},
						{
							$sort:{
								schedule:1
							}
						},
						function(err,docs){
							if(err) {
								console.log(err);
								return;
						};//if(err)
						cb(null,data0,data1,data2,docs);
					}

						);//aggregate()
			},
				function(data0,data1,data2,data3,cb){
					Private
					.aggregate(
						{
							$match:{
								schedule:{
									$gte:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(4,'days').format('L')).subtract(5,'hours').toDate(),
									$lt:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(5,'days').format('L')).subtract(5,'hours').toDate()
								}
							}//$match
						},
						{
							$project:{
								guestname:1,
								mobile:1,
								schedule:1,
								assistant:1
							}
						},
						{
							$sort:{
								schedule:1
							}
						},
						function(err,docs){
							if(err) {
								console.log(err);
								return;
						};//if(err)
						cb(null,data0,data1,data2,data3,docs);
					}

						);//aggregate()
			},
				function(data0,data1,data2,data3,data4,cb){
					Private
					.aggregate(
						{
							$match:{
								schedule:{
									$gte:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(5,'days').format('L')).subtract(5,'hours').toDate(),
									$lt:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(6,'days').format('L')).subtract(5,'hours').toDate()
								}
							}//$match
						},
						{
							$project:{
								guestname:1,
								mobile:1,
								schedule:1,
								assistant:1
							}
						},
						{
							$sort:{
								schedule:1
							}
						},
						function(err,docs){
							if(err) {
								console.log(err);
								return;
						};//if(err)
						cb(null,data0,data1,data2,data3,data4,docs);
					}

						);//aggregate()
			},
						function(data0,data1,data2,data3,data4,data5,cb){
					Private
					.aggregate(
						{
							$match:{
								schedule:{
									$gte:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(6,'days').format('L')).subtract(5,'hours').toDate(),
									$lt:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(7,'days').format('L')).subtract(5,'hours').toDate()
								}
							}//$match
						},
						{
							$project:{
								guestname:1,
								mobile:1,
								schedule:1,
								assistant:1
							}
						},
						{
							$sort:{
								schedule:1
							}
						},
						function(err,docs){
							if(err) {
								console.log(err);
								return;
						};//if(err)
						cb(null,[data0,data1,data2,data3,data4,data5,docs]);
					}

						);//aggregate()
			},

			],function(err,result){
				if(err) 
					{
						console.log(err);
						return;
					};//if(err)
				res.send({
					data:result
				});
			});
			// Private
			// .aggregate({
			// 	$match:{
			// 		schedule:{
					
			// 			$gte:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).format('L')).toDate(),
			// 			$lt:moment(moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).add(7,'days').format('L')).toDate()
					
			// 		}
			// 	}
			// }
			// ,{
			// 	$group:{
			// 		_id:'null',
			// 		// // totle:{
			// 		// // 	$sum:'$guestname'
			// 		// // }
			// 		schedule:{
						
			// 		}
			// 	}
			// }

			// ,function(err,docs){
			// 	if(err) console.log(err);
			// 	res.send({
			// 		data:docs
			// 	})
			// });//
			// .exec(function(err,result){
			// 	if(err) console.log(err);
			// 	res.send({
			// 		data:result
			// 	})
			// })
			// .find({
			// 	schedule:{
			// 		$gte:moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).subtract(5,'hours'),
			// 		$lt:moment(year+'-'+month+'-'+day+'T'+'00',moment.ISO_8601).subtract(5,'hours').add(7,'days')
			// 	}
			// })
			// .exec(function(err,result){
			// 	if(err) cosole.log(err);
			// 	res.send({
			// 		data:result
			// 	})
			// });
		};//if(req.session)
	},//ScheduleSearch()
	UpdateExercisePrePlan:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			data.schedule=moment(data.schedule,moment.ISO_8601);
			console.log(typeof data.schedule);
			console.log(data.schedule);
			var guestname=data.guestname;
			var mobile=data.mobile;
			var prePlanname=data.prePlanname;
			var schedule=data.schedule;
			Exercise
			.update({guestname:guestname,
					mobile:mobile,
					prePlanname:prePlanname},{$set:{schedule:schedule}},function(err,result){
						if(err) console.log(err);
						console.log(result);
						res.send({
							code:[1000,'success'],
							data:'成功'
						});
					})
		};
	},//UpdateExErcisePrePlan()

	SaveExercise:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			console.log(data.schedule);
			console.log(typeof data.schedule);


			data.schedule=moment(data.schedule,moment.ISO_8601);
				console.log(typeof data.schedule);
				console.log(data.schedule);
			var oNewExercise=new Exercise(data);
		 	oNewExercise.save(function(err){

		 			if (err) console.log(err);
		 	
		 	}).then(
		 		function(data){
		 			res.send({code:code.success});	
		 			},
		 		function(err){
		 			console.log(err);
		 			res.send({code:code.insertFail});
			});//save()
		}//if(req.session)
	},//SaveExercise

	FetchOneDay:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			var year=parseInt(data.year);
			var month=parseInt(data.month);
			var day=parseInt(data.day);

			async.waterfall([
							function(cb){
								var mom=year+'-'+month+'-'+day+'T'+'07:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								    // mom=moment.utc([year,month,day,7,0,0,0]);
								console.log(typeof mom);
								console.log(mom);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,result)
								})
								// .count(function(err,count){
								// 	cb(null,count);
								// })
							},
							function(count7,cb){
								var mom=year+'-'+month+'-'+day+'T'+'08:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,cb){
								var mom=year+'-'+month+'-'+day+'T'+'09:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count);
								// 	// console.log(count7,count);
								// })
							}
							,
							function(count7,count8,count9,cb){
								var mom=year+'-'+month+'-'+day+'T'+'10:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,cb){
								var mom=year+'-'+month+'-'+day+'T'+'11:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,cb){
								var mom=year+'-'+month+'-'+day+'T'+'12:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,cb){
								var mom=year+'-'+month+'-'+day+'T'+'13:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,count12,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,cb){
								var mom=year+'-'+month+'-'+day+'T'+'14:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,cb){
								var mom=year+'-'+month+'-'+day+'T'+'15:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,cb){
								var mom=year+'-'+month+'-'+day+'T'+'16:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,cb){
								var mom=year+'-'+month+'-'+day+'T'+'17:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,cb){
								var mom=year+'-'+month+'-'+day+'T'+'18:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,count18,cb){
								var mom=year+'-'+month+'-'+day+'T'+'19:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,count18,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count18,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,count18,
								count19,cb){
								var mom=year+'-'+month+'-'+day+'T'+'20:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,count18,count19,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count18,count19,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,count18,
								count19,count20,cb){
								var mom=year+'-'+month+'-'+day+'T'+'21:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,count18,count19,count20,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count18,count19,count20,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,count18,
								count19,count20,count21,cb){
								var mom=year+'-'+month+'-'+day+'T'+'22:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,count18,count19,count20,count21,result)
								})
								// .count(function(err,count){
								// 	cb(null,count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count18,count19,count20,count21,count);
								// 	// console.log(count7,count);
								// })
							},
							function(count7,count8,count9,count10,count11,count12,count13,
								count14,count15,count16,count17,count18,
								count19,count20,count21,count22,cb){
								var mom=year+'-'+month+'-'+day+'T'+'23:00:00.000+00:00';
									mom=moment(mom,moment.ISO_8601);
								Exercise
								.find({schedule:mom})
								.populate('prePlanContent')
								.then(function(result){
									cb(null,[count7,count8,count9,count10,count11,count12,count13,count14,count15,count16,
										count17,count18,count19,count20,count21,count22,result])
								})
								// .count(function(err,count){
								// 	cb(null,[count7,count8,count9,count10,count11,
								// 		count12,count13,count14,count15,count16,
								// 		count17,count18,count19,count20,count21,
								// 		count22,count]);
								// 	// console.log(count7,count);
								// })
							},
							],function(err,result){
								if(err) console.log(err);
								console.log(result);
								res.send({
									code:[1000,'success'],
									data:result
								});
			});//async
		}//if(req.session)
	},//FetchOneDay
	FetchYuYueFormGuestnameO:function(req,res,next){
		if(req.session){
			var data=req.body;
			console.log(data);
			Exercise
			.find({guestname:data.guestname})
			.then(function(data){
				console.log(data);
				res.send({
					code:[1000,'success'],
					data:data
				});
			},function(err){
				if(err) alert(err);
			})
		}//if(req.session)
	},//FetchYuYueFormGuestname
	FetchYuYueFormGuestname:function(req,res,next){
		if(req.session){
				var data=req.body;
				console.log(data);
				var guestname=data.guestname;
				var schedule=data.schedule
				Exercise
				.find({guestname:guestname,schedule:data.schedule})
				.then(function(data){
					console.log(data);
					var data=data[0];
					if(data.guestname){
							GuestPrePlan
							.find({
								guestname:data.guestname,
								prePlanname:data.prePlanname
							})
							.then(function(data){
								res.send({
									code:[1000,'success'],
									data:data[0]
								});
							});//Guestname
						}//if(data.guestname)
					// res.send({
					// 	code:[1000,'success'],
					// 	data:data[0]
					// })
				});
		}//if(req.session)
	},//FetchYuYueFormGuestname
	FetchYuYueFormSchedule:function(req,res,next){
		if(req.session){
				var data=req.body;
				console.log(data);
				var schedule=data.schedule;
				Exercise
				.find({schedule:data.schedule})
				.then(function(data){
					console.log(data);
					var containBox=new Array();
					

					// async.waterfall([function(cb){
					// 	data.forEach(function(v,i){
					// 	GuestPrePlan
					// 	.find({guestname:v.guestname})
					// 	.then(function(err,result){
					// 		containBox.concat(result);
					// 	});

					// 	}
					// }],function(err,result){

					// });//async.waterfall()
				
					// });//data.forEach()

					// GuestPrePlan
					// .find(function(gu))
					// async.waterfall([function(cb){

					// }],function(err,result){

					// });

					//async.parallel
					// var data=data[0];
					// if(data.guestname){
					// 		GuestPrePlan
					// 		.find({
					// 			guestname:data.guestname,
					// 			prePlanname:data.prePlanname
					// 		})
					// 		.then(function(data){
					// 			res.send({
					// 				code:[1000,'success'],
					// 				data:data[0]
					// 			});
					// 		});//Guestname
					// 	}//if(data.guestname)
					// res.send({
					// 	code:[1000,'success'],
					// 	data:data[0]
					// })
				});
		}
	},//FetchYuYueFormSchedule
	Population:function(req,res,next){
		if(req.session){
			
			///////////////////////////////////////////////////////////
			
			// var data=req.body;
			// console.log(data);
			// Exercise
			// .find({guestname:data.guestname})
			// .populate('guest')
			// .exec(function(err,result){
			// 	console.log(result);
			// 	res.send({
			// 		data:result[0].guest.prePlanMotions
			// 	});
			// });//Exercise()
			
			//////////////////////////////////////////////////////////

			//////////////////////////正式接口////////////////////////
			
			var data=req.body;
			console.log(data);
			Exercise
			.find({schedule:data.schedule})
			.populate('guest')
			.exec(function(err,result){
				console.log(result);
				res.send({
					data:result
				});
			});//Exercise()

			//////////////////////////////////////////////////////////

		};//if(req.session)
	},//Population
	FetchOneHour:function(req,res,next){
		if(req.session){
			var data=req.body.data;
			Exercise
			.find({schedule:data})
			.exec(function(err,result){
				if(err) console.log(err);
				res.send({
					data:result
				});
			})
		};//if(req.session)
	},//fetchOneHour() App 用
	VagueName:function(req,res,next){
		if(req.session){
			var data=req.body.vaguename;
			var regex=new RegExp(data);
   			Exercise
   			.find({'guestname':regex})
   			.exec(function(err,result){
   				if(err) console.log(err);
   				res.send({
   					code:[1000,'success'],
   					data:result
   				});
   			});//Exercise
		}//if(req.session)
	},//VagueName()
	ParseExcel:function(req,res,next){
	
		if(req.session){
			console.log(1);
			try{
			parseXlsx('test.xlsx',function(err,data){
				if(err) {
					console.log(err);
					return;
				}//if(err)
				console.log(data);
				res.send({data:data});
			});//
		}catch(e){
		if(e) console.log(e);			
		}
			
		};//if(req.session)
	},//ParseExcel
	IpadPrivateVaildSchedule:function(req,res,next){
		// Private
		// .
	},//IpadPrivateVaildSchedule
	MakeExcel:function(){
		// var obj  = {"worksheets":[{"data":[["姓名","性别","年龄"],["李晓龙","男","24"]]}]};
		// Guest
		// .aggregate(
		// {
		// 	$group:
		// 	{_id:'$'}
		// }
		// );//Guest
   		var file = nodeXlsx.build([{name:'king',data:[["姓名","性别","年龄"],["李晓龙","男","24"]]
   									}]);
   		fs.writeFileSync('guest.xlsx', file, 'binary');
	},//MakeExcel
}